package com.server.scheduler;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.server.date.DateUtil;
import com.server.service.IEmailSettingService;
import com.server.service.IFinanceService;
import com.server.service.IRewardConfigService;
import com.server.service.IRewardService;
import com.server.service.IUserManagementService;
import com.sun.org.apache.bcel.internal.generic.NEW;
@Component
/*@EnableScheduling  */
public class VerifyCleanInvoker {
	
	private ExecutorService threadPool = null;
	@Autowired
	private IRewardService rewardService;
	@Autowired
	private IUserManagementService userManagementService;
	@Autowired
	private IFinanceService financeService;
	@Autowired
	private IEmailSettingService emailSettingService;
	@Autowired
	private JdbcTemplate jdbcTemplate;
	@Autowired
	private IRewardConfigService rewardConfig;
	
	private DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");    
	
	public VerifyCleanInvoker(){
		threadPool = Executors.newCachedThreadPool(new ThreadFactory() {
			@Override
			public Thread newThread(Runnable r) {
				Thread thread = new Thread(r);
				thread.setDaemon(true);
				return thread;
			}
		});
	}
	
	 /**
	 * 每天23：30重新加载员工层级数据以保证数据的准确性
	 * @throws InterruptedException
	 * @author ServerZhang
	 * @date 2017年3月11日
	 */
	@Scheduled(cron = "0 30 23 * * ?")  
	public void refreshLevelsData() throws InterruptedException{
            threadPool.execute(new Runnable(){
                @Override 
                public void run() { 
                	System.out.println(dateFormat.format(new Date())+" 定时任务： 重新加载员工层级数据");
                	rewardService.loadData();
                } 
            });            
	}
	/**
	 * 每天00：00计算并保存返费金额
	 * 同时设置次日考勤状态为未考勤
	 * @throws InterruptedException
	 * @author ServerZhang
	 * @date 2017年3月11日
	 */
	@Scheduled(cron = "0 0 0 * * ?")  
	//@Scheduled(cron = "0 0/1 * * * ?")  
	public void reward() throws InterruptedException{
		threadPool.execute(new Runnable(){
			@Override 
			public void run() { 
				System.out.println(dateFormat.format(new Date())+" 定时任务： 计算并保存返费金额");
				rewardService.reward();
				userManagementService.iniCheckIn();
			} 
		});            
	}
	/**
	 * 每天08：30向财务发打款邮件
	 * @throws InterruptedException
	 * @author ServerZhang
	 * @date 2017年3月25日
	 */
	@Scheduled(cron = "0 30 8 * * ?")  
	//@Scheduled(cron = "0 0/1 * * * ?")  
	public void sendEmail() throws InterruptedException{
		threadPool.execute(new Runnable(){
			@Override 
			public void run() { 
				Integer count = financeService.getCount(1);
				if (count > 0) {
					System.out.println(dateFormat.format(new Date())+" 定时任务： 向财务发打款邮件");
					emailSettingService.sendEmail(1, count, null);
				}
			} 
		});            
	}
	/**
	 * 每天17：00向管理员发审核申请
	 * @throws InterruptedException
	 * @author ServerZhang
	 * @date 2017年3月25日
	 */
	@Scheduled(cron = "0 0 17 * * ?")  
	//@Scheduled(cron = "0 0/1 * * * ?")  
	public void sendEmail2() throws InterruptedException{
		threadPool.execute(new Runnable(){
			@Override 
			public void run() { 
				Integer count = financeService.getCount(0);
				if (count > 0) {
					System.out.println(dateFormat.format(new Date())+" 定时任务： 向管理员发审核申请");
					emailSettingService.sendEmail(2, count, null);
				}
			} 
		});            
	}
	/**
	 * 每隔5秒执行一次
	 * @throws InterruptedException
	 * @author ServerZhang
	 * @date 2017年7月5日
	 */
	//@Scheduled(cron = "0/10 * * * * ?")  
	@Scheduled(cron = "30 47 15 * * ?")  
	public void test() throws InterruptedException{
		threadPool.execute(new Runnable(){
			@Override 
			public void run() { 
				System.out.println(dateFormat.format(new Date())+" 定时任务：功能测试开始");
				
				//System.out.println(dateFormat.format(new Date())+" 定时任务： 计算并保存返费金额");
				//rewardService.reward();
				//userManagementService.iniCheckIn();
				
				System.out.println(dateFormat.format(new Date())+" 定时任务：功能测试结束");
			} 
		});            
	}
	
	@Scheduled(cron = "30 03 15 * * ?")  
	public void test1() throws InterruptedException{
		threadPool.execute(new Runnable(){
			@Override 
			public void run() { 
				/*批量修改 错误的返费政策*/
				/*System.out.println(dateFormat.format(new Date())+" 定时任务：功能测试开始");
				String sql = "SELECT * FROM reward.employ WHERE CreateTime > '2017-06-14' ORDER BY CreateTime DESC";
				List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
				for (Map<String, Object> map : list) {
					if ( map.size()>0 && map!=null ) {
						String CardId = map.get("CardId").toString();
						String OrgNo = map.get("OrgNo").toString();
						Date CreateTime = (Date) map.get("CreateTime");
						Integer Nature = (Integer) map.get("Nature");
						Integer SourceType = (Integer) map.get("SourceType");
						String RewardId = map.get("RewardId").toString();
						String newRewardId = "";
						if (!RewardId.equals("9")) {
							continue;
						}
						List<Map<String, Object>> rewardConfigList = rewardConfig.getList(OrgNo, DateUtil.toDate(CreateTime.toString()),Nature,SourceType);
						if (rewardConfigList.size() > 0) {
							Map<String, Object> configMaap = rewardConfigList.get(0);//公司的最新返费配置
							newRewardId = configMaap.get("Id").toString();
						}
						if ( StringUtils.isEmpty(newRewardId) ) {
							newRewardId="0";
						}
						String updatesql = "update reward.employ set RewardId="+newRewardId+" where CardID='"+CardId+"'";
						int updateResult = jdbcTemplate.update(updatesql);
						System.out.println(CardId+"--"+OrgNo+"--"+CreateTime+"--"+Nature+"--"+SourceType+"--"+RewardId+"--"+newRewardId+"---------"+updateResult);
					}
				}
				System.out.println(dateFormat.format(new Date())+" 定时任务：功能测试结束");*/
			} 
		});            
	}
}
